<?php
//******************************************************************
function genPass($MaxL) {
	$chars = "qazxswedcvfrtgbnhyujmkilp123456789QAZXSWEDCVFRTGBNHYUJMKILP";
	$size = StrLen($chars)-1;
	mt_srand (time()+(double)microtime()*1000000);
	$password = null;
	while($MaxL--) $password.=$chars[rand(0,$size)];
	return $password;
}
//******************************************************************
function quick_strtr($str, $replace) {
	$from = array_keys ($replace);
	$to = array_values ($replace);
	return str_replace ($from, $to, $str);
}
//******************************************************************
function sendMail($to, $from, $sbj, $text) {
	$head = "From: $from\r\n"."Content-Type: text/plain; charset=\"UTF-8\"\r\n";
	return @Mail($to, $sbj, $text, $head);
}
//******************************************************************
if (!function_exists('strptime')) {
	function strptime($timestr, $format) {
		$matches=array();
		
		preg_match_all('/%[a-zA-Z]/',$format,$matches);
		$matches=$matches[0];
		$regexp=$format;
		$outParams=array();
		
		foreach ($matches as $modifier) {
			switch ($modifier) {
				case '%d':
					$outParams[]='tm_mday';
					$regexp=str_replace($modifier,'(\d{2})',$regexp);
					break;
				case '%m':
					$outParams[]='tm_mon';
					$regexp=str_replace($modifier,'(\d{2})',$regexp);
					break;
				case '%Y':
					$outParams[]='tm_year';
					$regexp=str_replace($modifier,'(\d{4})',$regexp);
					break;
				case '%H':
					$outParams[]='tm_hour';
					$regexp=str_replace($modifier,'(\d{2})',$regexp);
					break;
				case '%M':
					$outParams[]='tm_min';
					$regexp=str_replace($modifier,'(\d{2})',$regexp);
					break;
				case '%S':
					$outParams[]='tm_sec';
					$regexp=str_replace($modifier,'(\d{2})',$regexp);
					break;
			}
		}
		
		$regexp='/^'.$regexp.'$/';
		
		$outArray=array(
			'tm_sec'=>date('s'),
			'tm_min'=>date('i'),
			'tm_hour'=>date('H'),
			'tm_mday'=>date('d'),
			'tm_mon'=>date('m'),
			'tm_year'=>(date('Y')-1900)
		);
		
		if (!preg_match_all($regexp,$timestr,$matches))
			return false;

		foreach ($outParams as $i=>$paramName) {
			$value=$matches[$i+1][0];
			if ($paramName=='tm_year')
				$value-=1900;
			elseif ($paramName=='tm_mon')
				$value-=1;
			
			$outArray[$paramName]=$value;
		}
		
		return $outArray;
	}
}
//******************************************************************
function counter($name, $increment=true, $reset=false) {
	$fullName = "counter_".$name;
	
	if (!isset($GLOBALS[$fullName])) {
		$GLOBALS[$fullName] = 0;
		return 0;
	} else {
		if ($reset) {
			unset($GLOBALS[$fullName]);
			return;
		}
		if ($increment) $GLOBALS[$fullName]++;
		return $GLOBALS[$fullName];
	}
}
//******************************************************************
function getFileList($dir) {
	$fileList = array();
	if ($handle = @opendir($dir)) {
		while ((($file = readdir($handle)) !== false)) {
			if (substr($file, 0, 1) != ".") {	// exclude ".", ".." and ".svn"
				$fileList[] = $dir.$file;
			}
		}
		closedir($handle);
	}
	return $fileList;
}
//******************************************************************
function getFileListRecursive($start_dir, $extension=false) {
	$files = array();
	if (is_dir($start_dir)) {
		$fh = opendir($start_dir);
		while (($file = readdir($fh)) !== false) {
			if (substr($file, 0, 1) == ".") continue;	// exclude ".", ".." and ".svn"
			$filepath = $start_dir . '/' . $file;
			if ( is_dir($filepath) ) {
				$files = array_merge($files, getFileListRecursive($filepath, $extension));
			} else {
				if (!$extension or ($extension and strpos($filepath, $extension))) {
					array_push($files, $filepath);
				}
			}
		}
		closedir($fh);
	} else {
		$files = false;
	}
	return $files;
}
//******************************************************************
function stripTagsAndTrim($text, $maxLen=300) {
	$stripped = strip_tags($text);
	if (strlen($stripped) > $maxLen) {
		return substr($stripped, 0, $maxLen)." …";
	} else {
		return $stripped;
	}
}
//******************************************************************
function is_natural($number) {
	return (boolean)preg_match('/^[0-9]{1,10}$/is', $number);
}
//******************************************************************
?>